﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wp.DAL;
using Wp.BLL;
using System.Text;
using System.Web.Caching;

public partial class Controls_TopMenu : System.Web.UI.UserControl
{
    string CacheKey = "menucache";
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
    {
        if (HttpRuntime.Cache[this.CacheKey] == null)
        {
            this.CacheMenu();
        }
    }

    private void CacheMenu()
    {
        string str = this.LoadChildMenu(-1);
        HttpRuntime.Cache.Insert(this.CacheKey, str, null, DateTime.UtcNow.AddMinutes(10.0), TimeSpan.Zero, CacheItemPriority.Normal, new CacheItemRemovedCallback(this.CachedItemRemoveCallBack));
    }

    public string GetMenu()
    {
        if (HttpRuntime.Cache[this.CacheKey] == null)
        {
            this.CacheMenu();
        }
        return HttpRuntime.Cache[this.CacheKey].ToString();
    }

    public string LoadChildMenu(int ParentId)
    {
        List<TblPostsEntity> list = TblPostsManager.CreateInstant().SelectTblPostByParent(ParentId, "menu");
        StringBuilder builder = new StringBuilder();
        builder.Append(" <ul>");
        for (int i = 0; i < list.Count; i++)
        {
            List<TblPostsEntity> list2 = TblPostsManager.CreateInstant().SelectTblPostByParent(list[i].Id, "menu");
            builder.Append("<li class='menu-item'><a href=\"" + list[i].Url + "\" title='" + list[i].Title + "'>" + list[i].Title + "</a>");
            if (list2.Count > 0)
            {
                builder.Append(this.LoadChildMenu(list[i].Id));
            }
            builder.Append("</li>");
        }
        builder.Append(" </ul>");
        return builder.ToString();
    }
}